var BASE_URL = 'http://jinzhidata.com/v2'

//读取cookie
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) {
        return unescape(arr[2]);
    }
    return null;
}


$(function () {
    var url = window.location.href
    $.ajax({
        type: "get",
        url: BASE_URL + "/share/getShareConfig",
        dataType: "json",
        async: false,
        data: {'url': url},
        success: function (res) {
            wx.config({
                debug: false,////生产环境需要关闭debug模式
                appId: res.data.appid,//appId通过微信服务号后台查看
                timestamp: res.data.timestamp,//生成签名的时间戳
                nonceStr: res.data.nonceStr,//生成签名的随机字符串
                signature: res.data.signature,//签名
                jsApiList: [//需要调用的JS接口列表
                    'checkJsApi',//判断当前客户端版本是否支持指定JS接口
                    'onMenuShareTimeline',//分享给好友
                    'onMenuShareAppMessage'//分享到朋友圈
                ]
            });
        },
        error: function (xhr, status, error) {
            alert(status);
            //alert(xhr.responseText);
        }
    })

    // 获取资源id
    var resourceId = new RegExp(BASE_URL + "/resource\\.html\\?resourceId=(.*)").exec(url)[1]
    // var resourceId = 'c09ac06397ec4a0ea2050ab99963a70c'
    var resourcePrice = -1;
    // 加载资源详情
    $.ajax({
        url: BASE_URL + "/resource/" + resourceId,
        type: "get",
        contentType: 'application/json',
        dataType: "json",
        success: function (response) {
            var data = response.data
            $("#resourceCover").attr("src", '/img/' + data.resourceCover)
            $("#resourceName").html(data.resourceName)
            $("#resourceAuthor").html('发布者：' + data.resourceAuthorName)
            $("#resourcePrice").html(data.resourcePrice + "积分")
            resourcePrice = data.resourcePrice
            $("#resourceIntroduction").html(data.resourceIntroduction)
            $("#resourceDetail").html(data.resourceDetail)
            wx.ready(function () {
                //分享朋友圈
                wx.onMenuShareTimeline({
                    title: data.resourceName,
                    link: url,
                    imgUrl: data.resourceCover,// 自定义图标
                    success: function (res) {
                    }
                });
                //分享给好友
                wx.onMenuShareAppMessage({
                    title: data.resourceName,
                    desc: '${resource.resource_introduction}', // 分享描述
                    link: url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                    imgUrl: data.resourceCover,// 自定义图标
                    success: function () {
                        // 用户确认分享后执行的回调函数
                    }
                });
                wx.error(function (res) {
                    alert(res.errMsg);
                });
            });
        }
    })

    var htmlBuy =
        '        <span style="padding:14px;background-color:#f2b300;color:white;font-size:14px;width: 40%;text-align: center" id="buy">去支付</span>\n' +
        '        <span style="padding-right:10px;font-size:14px">您尚未购买该资源</span>';
    var htmlDownload =
        '        <span style="padding:14px;background-color:#f2b300;color:white;font-size:14px;width: 40%;text-align: center" id="download" >下载</span>\n' +
        '        <span style="padding-right:10px;font-size:14px">您已购买该资源</span>'
    $.ajax({
        type: 'get',
        url: BASE_URL + '/order/getOrderState',
        async: false,
        data: {'resourceId': resourceId},
        success: function (result) {
            if (result.data === "0") {
                document.getElementById("foot").innerHTML = htmlBuy;
            } else {
                document.getElementById("foot").innerHTML = htmlDownload;
            }
        }
    });
    $("#buy").click(function () {
        if (!confirm("确定要使用" + resourcePrice + "积分购买该资源吗？")) {
            return;
        }
        $.ajax({
            type: 'get',
            url: BASE_URL + "/order/pay?resourceId=" + resourceId,
            async: false,
            success: function (result) {
                if (result.code === 0) {
                    alert("购买成功！");
                    window.location.reload()
                } else if (result.code === 1001) {
                    alert("您还没有登录！")
                    location.href = BASE_URL + "/user.html"
                } else {
                    alert(result.msg)
                }
            }
        });
    })
    $("#download").click(function () {
        // location.href = BASE_URL + "/resource/download/" + resourceId + "?accessToken=" + getCookie("accessToken")
        location.href = BASE_URL + "/download_resource.html?resourceId=" + resourceId + "&accessToken=" + getCookie("accessToken")
    })
})


